package com.qms.qep.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.enums.BusinessType;
import com.qms.qep.domain.InspectionAttachment;
import com.qms.qep.service.IInspectionAttachmentService;

/**
 * 检验附件Controller
 * 
 * @author qms
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/qep/inspection/attachment")
public class InspectionAttachmentController extends BaseController
{
    @Autowired
    private IInspectionAttachmentService inspectionAttachmentService;

    /**
     * 根据任务ID查询检验附件列表
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:query')")
    @GetMapping("/list/{taskId}")
    public AjaxResult list(@PathVariable("taskId") Long taskId)
    {
        List<InspectionAttachment> list = inspectionAttachmentService.selectInspectionAttachmentByTaskId(taskId);
        return success(list);
    }

    /**
     * 获取检验附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(inspectionAttachmentService.selectInspectionAttachmentById(id));
    }

    /**
     * 上传检验附件
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:edit')")
    @Log(title = "检验附件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file, Long taskId, String remark)
    {
        try {
            // 上传文件
            InspectionAttachment attachment = inspectionAttachmentService.uploadFile(file, taskId, remark);
            return success(attachment);
        } catch (Exception e) {
            logger.error("上传文件失败", e);
            return error("上传文件失败：" + e.getMessage());
        }
    }

    /**
     * 删除检验附件
     */
    @PreAuthorize("@ss.hasPermi('qep:inspection:remove')")
    @Log(title = "检验附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(inspectionAttachmentService.deleteInspectionAttachmentById(id));
    }
} 